package org.nimi317.web_gis.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nimi317.web_gis.dao.QualityModelDao;
import org.nimi317.web_gis.entity.Model;
import org.nimi317.web_gis.entity.QualityModel;
import org.nimi317.web_gis.form.post.QualityModelPost;
import org.nimi317.web_gis.service.ModelService;
import org.nimi317.web_gis.service.QualityModelService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * (QualityModel)表服务实现类
 *
 * @author makejava
 * @since 2024-05-23 08:42:27
 */
@Service("qualityModelService")
public class QualityModelServiceImpl extends ServiceImpl<QualityModelDao, QualityModel> implements QualityModelService {

    private final ModelService modelService;

    public QualityModelServiceImpl(ModelService modelService) {
        this.modelService = modelService;
    }

    @Override
    public IPage<QualityModel> pageData(QualityModelPost post) {
        LambdaQueryWrapper<QualityModel> wrapper = new LambdaQueryWrapper<>();
        Optional.ofNullable(post.getType()).ifPresent(item -> {
            wrapper.eq(QualityModel::getType, item);
        });

        Optional.ofNullable(post.getModelId()).ifPresent(item -> {
            wrapper.eq(QualityModel::getModelId, item);
        });

        Optional.ofNullable(post.getId()).ifPresent(item -> {
            wrapper.eq(QualityModel::getId, item);
        });
        IPage<QualityModel> page = this.page(null, wrapper);
        List<QualityModel> records = page.getRecords();
        if(ObjectUtil.isNotEmpty(records)) {
            List<Integer> modelList = records.stream().map(QualityModel::getModelId).toList();
            List<Model> models = modelService.listByIds(modelList);
            page.setRecords(records.stream().peek(item -> item.setModel(models.stream().filter(model -> model.getId().equals(item.getModelId())).toList().get(0))).toList());
        }
        return page;
    }
}

